package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.customview.turntable.StkTextLineToCenterAdapter;
import com.stark.customview.turntable.StkTurnTableItem;
import com.stark.customview.turntable.StkTurnTableView;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityTurntableBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

public class TurntableActivity extends BaseAc<ActivityTurntableBinding> {

    private String[] dataArray;
    private List<StkTurnTableItem> listData = new ArrayList<>();
    private StkTextLineToCenterAdapter adapter;
    private Dialog myCustomDialog;
    private EditText dialogEdit;

    @Override
    protected int onCreate() {
        return R.layout.activity_turntable;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivTurntableBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivStart.setOnClickListener(this);
        mDataBinding.ivTurntableEdit.setOnClickListener(this);

        mDataBinding.turnTableContainer.setListener(new StkTurnTableView.IListener() {
            @Override
            public void onRotateStart() {
                //开始转动时的回调
            }

            @Override
            public void onRotateEnd() {
                //转动结束时的回调
            }

            @Override
            public void onSelItem(int pos) {
                //转动结束时，指针所指向的数据位置
                StkTurnTableItem item = (StkTurnTableItem) mDataBinding.turnTableContainer.getAdapter().getItem(pos);
                mDataBinding.tvTurntableResult.setText(item.text);
            }
        });

        adapter = new StkTextLineToCenterAdapter();
        adapter.setTextHOffset(-20);

        //自定义弹框
        myCustomDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivStart://开始
                //转动
                mDataBinding.turnTableContainer.startRotate();
                break;
            case R.id.ivTurntableEdit://自定义
                myCustomDialog.show();
                break;
            //设置名字弹框
            case R.id.ivDialogCustomBack://返回
                myCustomDialog.dismiss();
                break;
            case R.id.ivDialogCustomSave://保存
                String data = dialogEdit.getText().toString().trim();
                if (TextUtils.isEmpty(data)) {
                    ToastUtils.showShort(getString(R.string.please_input_option));
                    return;
                }
                SPUtil.putString(mContext, Content.TURNTABLE_DATA, data);
                getData();
                myCustomDialog.dismiss();
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        String data = SPUtil.getString(mContext, Content.TURNTABLE_DATA, getString(R.string.turntable_data));
        listData.clear();
        dialogEdit.setText(data);
        dataArray = data.split("，");
        StkTurnTableItem item;
        for (int i = 0; i < dataArray.length; i++) {
            item = new StkTurnTableItem();
            item.text = dataArray[i];
            item.bgColor = i % 2 == 0 ? Color.parseColor("#FEE8C9") : Color.parseColor("#FAFAFA");
            item.textColor = Color.parseColor("#232323");
            item.textSize = SizeUtils.sp2px(14);
            listData.add(item);
        }
        adapter.setDataList(listData);
        mDataBinding.turnTableContainer.setAdapter(adapter);
    }

    //自定义弹框
    private void myCustomDialog() {
        myCustomDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_custom, null);
        myCustomDialog.setContentView(inflate);
        myCustomDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myCustomDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView back = inflate.findViewById(R.id.ivDialogCustomBack);
        ImageView save = inflate.findViewById(R.id.ivDialogCustomSave);
        dialogEdit = inflate.findViewById(R.id.etData);
        back.setOnClickListener(this);
        save.setOnClickListener(this);
    }

}